//
// Created by hay boy on 2022/6/27.
//

#ifndef ALGORITHMS_PRINTMATCHEDPAIRS_H
#define ALGORITHMS_PRINTMATCHEDPAIRS_H
#include "../stack/ArrayStack.h"
/**
 * 对一个字符串的左右进行匹配
 * **/
void printMatchedPairs(string expr);
void printMatchedPairs(string expr){
    ArrayStack<int> s;
    int length = (int)expr.size();
    for(int i = 0;i<length;i++){
        if(expr.at(i)=='('){
            s.push(i);
        }else{
            if(expr.at(i)==')'){
                try{
                    cout<<s.top()<<' '<<i<<endl;
                    s.pop();
                }catch (StackEmpty&){
                    cout<<"No match for right parenthesis."<<" at "<<i<<endl;
                }
            }
        }
    }
    while(!s.empty()){
        cout<<"No match for left parenthesis at "<<s.top()<<endl;
        s.pop();
    }

}
#endif //ALGORITHMS_PRINTMATCHEDPAIRS_H
